package DTaskTowerApp::Controller::Tower;
use Mojo::Base 'Mojolicious::Controller';
use 5.018;
use warnings;
use Tower::API;
use DTask::Task;
use Mango;
my $mango = Mango->new;
use Mojo::JSON qw(true false);
use DTask::Utils qw(renderJsonResponse);

sub getAccessToken
{
	my $c = shift;
	my $username = $c->param('username') // '';
	my $password = $c->param('password') // '';
	warn "username ||$username||";
	warn "password ||$password||";
	Tower::API::getAccessToken($username, $password, sub {
		my ($doc)= @_;
		$c->render(json => $doc);
	});
}

sub getTeamMembers
{
	my $c = shift;
	my $headers = $c->tx->req->headers;
	my $access_token = $headers->header('Tower-Token');
	Tower::API::getTeamMembers($access_token, sub {
		my ($doc)= @_;
		$c->render(json => $doc);
	});
}

sub getProjects
{
	my $c = shift;
	my $headers = $c->tx->req->headers;
	my $access_token = $headers->header('Tower-Token');
	Tower::API::getProjects($access_token, sub {
		my ($doc)= @_;
		$c->render(json => $doc);
	});
}

sub getTodoLists
{
	my $c = shift;
	my $headers = $c->tx->req->headers;
	my $access_token = $headers->header('Tower-Token');
	my $project_guid = $c->stash('project_guid');
	Tower::API::getTodoLists($access_token, $project_guid, sub {
		my ($doc)= @_;
		$c->render(json => $doc);
	});

}

sub addTodoList
{
	my $c = shift;
	my $project_guid = $c->stash('project');
	my $title = $c->param('title');
	Tower::API::addTodoList($project_guid, $title, sub {
#		my ($ua, $tx)= @_;
#		if ( $tx->success ) {
#			my $new_todolist = $tx->res->json;
#			my ($guid,$name) = @$new_todolist{qw(guid name)};
#			$c->render(text => 'add todo list '. $guid );
#		}
#		else
#		{
#			my $err = $tx->error;
#			if ( $err->{code})
#			{
#				warn "failed to create todolist code : $err->{code} $err->{message}";
#			}
#		}
#		

	});
}

sub addTodo
{
	my $c = shift;
	my $todolist_guid = $c->param('todolist');
	my $content = $c->param('content');
	my $assignee_guid = $c->param('assignee');
	my $due_at = $c->param('due_at');
	Tower::API::create_todo($todolist_guid, $content, $assignee_guid, $due_at,  sub {
		my ($ua, $tx) = @_;
		if ($tx->success)
		{
			my $new_todo = $tx->res->json;
			my ($guid,$name) = @$new_todo{qw(guid name)};

			say "success add todo $guid";
			
			$c->render(text => "success add todo $guid");
		}
		else
		{
			my $err = $tx->error;
			$c->render(text => "failed ". $err->{message});
		}
			
	});
}

sub addTodoComment
{
	my $c = shift;
	my $todo_guid = $c->param('todo');
	my $content = $c->param('content');
	Tower::API::create_todo_comment($todo_guid, $content , sub {
		my ($ua, $tx) = @_;
		if ($tx->success)
		{
			say "success add todo comment";
			$c->render(text => "success add todo comment");
		}
		else
		{
			my $err = $tx->error;
			$c->render(text => "failed ". $err->{message});
		}
	});
}

sub addTodoListComment
{
	my $c = shift;
	my $todolist_guid = $c->stash('todolist');
	my $content = $c->param('content');
	Tower::API::create_todolist_comment($todolist_guid, $content , sub {
		my ($ua, $tx) = @_;
		if ($tx->success)
		{
			say "success add todolist comment";
			$c->render(text => "success add todolist comment");
		}
		else
		{
			my $err = $tx->error;
			$c->render(text => "failed ". $err->{message});
		}
	});
}

sub newTaskWithTowerTodo
{
	my $c = shift;
	my $todo_guid = $c->stash('todo_guid');
	warn 'NewTaskWithTowerTodo';
	DTask::Task::setMango($mango);
	DTask::Task::newWithTowerTodo($todo_guid, sub {
		my ($task_id) = @_;
		warn "task id $task_id";
		if ($task_id)
		{
			$c->render( json => {error => false, result => $task_id} );
		}
		else
		{
			$c->render( json => {error => true , error_message => "invalid todo guid $todo_guid" });
		}
	});
}



sub getTaskByTowerTodo
{
	my $c = shift;
	my $todo_guid = $c->stash('todo_guid');
	DTask::Task::setMango($mango);
	DTask::Task::getInfoByTowerTodo($todo_guid, sub {
		renderJsonResponse($c, @_);
	});
}

sub getTaskLinksInfoByTowerTodo
{
	my $c = shift;
	DTask::Task::setMango($mango);
	my $todo_guid = $c->stash('todo_guid');
	DTask::Task::getLinksInfoByTowerTodo($todo_guid, sub {
		my (@info)	= @_;
		$c->render(json => {error => false, result => \@info});
	});

}

1;
